package com.allwees.bs.c.module.order.context.compute.strategy.coupon;

import com.allwees.bs.c.module.order.context.bo.CouponComputeBo;
import com.allwees.bs.c.module.order.context.bo.CouponComputeItemBo;
import com.allwees.bs.c.module.order.context.bo.UserCouponBo;
import com.allwees.core.common.util.MoneyUtil;

import java.math.BigDecimal;

public class CouponCashStrategy extends CouponStrategy {

    @Override
    public boolean isSupport(UserCouponBo coupon) {
        return false;
    }

    @Override
    public CouponComputeBo use(UserCouponBo coupon, CouponComputeBo bo) {
        if (MoneyUtil.isNullOrZero(bo.getAmtCash())) {
            return bo;
        }
        //cash余额和实付金额比较,取最小的
        BigDecimal realAmtCash = MoneyUtil.min(bo.getAmtCash(), bo.getAmt());
        return useDeductAmt(realAmtCash, bo);
    }

    private static CouponComputeBo useDeductAmt(BigDecimal maxDeductAmt, CouponComputeBo computeBo) {

        BigDecimal orderTotal = computeBo.getAmt();//实付金额
        BigDecimal cashLeft = computeBo.getAmtCash(); //账户上总共的cash余额

        BigDecimal totalAmt = computeBo.getAmt();//临时变量
        BigDecimal tmpAmt = computeBo.getAmt();//临时变量


        if (orderTotal.compareTo(cashLeft) <= 0) {//订单实付金额小于cash余额
            cashLeft = MoneyUtil.sub(cashLeft, orderTotal);
            totalAmt = orderTotal;//amtCash为订单金额
            orderTotal = BigDecimal.ZERO;//订单实付为0

        } else {

            totalAmt = cashLeft; // amtCash为抵扣的cash
            orderTotal = MoneyUtil.sub(orderTotal, cashLeft);//订单实付(paypal需要付的钱) =订单金额 - amtCash
            cashLeft = BigDecimal.ZERO;
        }

        computeBo.setAmt(orderTotal);
        computeBo.setAmtCash(totalAmt);

        //BigDecimal  amtSku = BigDecimal.ZERO;
        //计算sku的cash实付
        int length = computeBo.getItems().size();
        BigDecimal amtCashSku = BigDecimal.ZERO;
        for (int i = 0; i < length; i++) {
            CouponComputeItemBo item = computeBo.getItems().get(i);

            BigDecimal ratio = ratio(item.getAmt(), tmpAmt);
            //sub(computeBo.getAmtCash(), amtCashSku) 是最后一个商品需要付的钱 (总共需要付的cash 减去 之前付的cash, 就是最后一个商品的cash)
            item.setAmtCashSku(last(length, i) ? sub(computeBo.getAmtCash(), amtCashSku) : mul(computeBo.getAmtCash(), ratio));//计算sku的amtCash
            amtCashSku = MoneyUtil.add(amtCashSku, item.getAmtCashSku());
            // sku实付(paypal需要付的钱) = ( 商品原金额 - coupon优惠 - shipping运费优惠 )  - cash 优惠
            item.setAmt(MoneyUtil.sub(item.getAmt(),item.getAmtCashSku()));

            if (MoneyUtil.isNullOrZero(item.getAmtShipping())) {
                item.setAmtCashShippingSku(BigDecimal.ZERO);
            } else {
                BigDecimal cashShippingRatio = ratio(item.getAmtShipping()/** amtSku 是商品实付金额(没有减cash) **/, tmpAmt /** amt 是 商品实付金额 + 运费实付金额之和(没有减cash)   **/);
                // cash付运费
                item.setAmtCashShippingSku(mul(item.getAmtCashSku(),cashShippingRatio));
            }

            // cash付商品
            item.setAmtCashProductSku(sub(item.getAmtCashSku(),item.getAmtCashShippingSku()));

            // sku商品实付,相当于amt_product_sku
            //item.setAmtSku(MoneyUtil.sub(item.getAmtSku(),item.getAmtCashProductSku()));
            // sku运费实付
            //item.setAmtShipping(MoneyUtil.sub(item.getAmtShipping(),item.getAmtCashShippingSku()));

            //累计运费
            //amtShipping = MoneyUtil.add(amtShipping, item.getAmtShipping());
            //累计商品
            //amtSku = MoneyUtil.add(amtSku, item.getAmtSku());
        }
        //computeBo.setAmtProduct(amtSku);
        //computeBo.setAmtShipping(amtShipping);
        return computeBo;

        /*
        //订单总金额
        BigDecimal orderAmount = computeBo.getAmt();

        //赠送现金金额
        BigDecimal amtCashBalanc = computeBo.getAmtCash();

        //当现金券大于0时,表示可以用现金支付
        if (amtCashBalanc != null && BigDecimal.ZERO.compareTo(amtCashBalanc) < 0) {

            if (orderAmount.compareTo(amtCashBalanc) > 0) {


            }
        }

         */


        /**
         *
         List<CouponComputeItemBo> items = computeBo.getItems();

         BigDecimal ratio = div(maxDeductAmt, computeBo.getAmt());

         BigDecimal amtShipping = BigDecimal.ZERO, amtSku = BigDecimal.ZERO, amtSaleTax = BigDecimal.ZERO, amtCash = BigDecimal.ZERO;
         int length = items.size();
         for (int i = 0; i < length; i++) {
         CouponComputeItemBo item = items.get(i);

         item.setAmtSaleTax(realAmtProduct(item.getAmtSaleTax(), ratio));

         item.setAmtShipping(realAmtProduct(item.getAmtShipping(), ratio));

         item.setAmtSku(last(length, i) ? sub(sub(computeBo.getAmt(), maxDeductAmt), amtSku) : realAmtProduct(item.getAmtSku(), ratio));
         item.setAmtCash(MoneyUtil.sub(item.getAmt(), item.getAmtSku(), item.getAmtShipping(), item.getAmtSaleTax()));
         item.computeAmt();

         //累计运费
         amtShipping = MoneyUtil.add(amtShipping, item.getAmtShipping());
         //累计商品
         amtSku = MoneyUtil.add(amtSku, item.getAmtSku());
         //累计税费
         amtSaleTax = MoneyUtil.add(amtSaleTax, item.getAmtSaleTax());

         amtCash = MoneyUtil.add(amtCash, item.getAmtCash());
         }
         computeBo.setAmtProduct(amtSku);
         computeBo.setAmtShipping(amtShipping);
         computeBo.setAmtSaleTax(amtSaleTax);
         computeBo.setAmtCash(amtCash);
         **/

        // return computeBo.computeAmt();
    }


    private static BigDecimal ratio(BigDecimal amtSku, BigDecimal amt) {
        return MoneyUtil.div(amtSku, amt, 10);
    }
}

